package com.springboot.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.demo.entity.BorrowMsg;
import com.springboot.demo.entity.Reader;
import com.springboot.demo.mapper.ReaderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ReaderService extends ServiceImpl<ReaderMapper, Reader> {
    @Autowired
    private ReaderMapper readerMapper;

    public Reader login(String phone, String password) {
        return readerMapper.findreader(phone, password);
    }

    public Reader getReader(String name){
       return baseMapper.selectOne(new LambdaQueryWrapper<Reader>().eq(Reader::getRname,name));
    }

    public void updateInfo(String oname, String opassword, String name, String password) {
        baseMapper.updateInfoReader(oname,opassword,name,password);
        baseMapper.updateInfoBorrow(oname, opassword, name, password);
        baseMapper.updateInfoReaderReaderFineRecord(oname, opassword, name, password);
        baseMapper.updateInfoReaderReserve(oname, opassword, name, password);
    }

    public Reader getReaderByName(String username) {
        return baseMapper.findByName(username);
    }

    public Reader getReaderByPhone(String phone) {
        return baseMapper.findByPhone(phone);
    }

    public Reader findPassword(String email){
        return baseMapper.getPassword(email);
    }

    public void payReaderFine(String phone, int totalFine) {

        readerMapper.payReaderFine(phone);
        readerMapper.decFine(phone, totalFine);
    }

    public void addReader(String phone,String rname,String password,String email, String date, Integer number, Integer deposit, String register)
    {readerMapper.addReader(phone, rname, password, email, date, number, deposit, register); }

    public void payDeposit(int deposit,String phone){readerMapper.payDeposit(phone, deposit);}

    public List<Reader> getall(){return readerMapper.getall();}

    //查询需要归还的书籍
    public List<BorrowMsg> getBook(String phone){
        return readerMapper.getBooks(phone);
    }

    public void Modify(String ophone,String phone,String name,String password)
    {
        readerMapper.modify_NP(ophone,name,password);
        readerMapper.modify_Phone(ophone, phone);
    }

    public void Delete(String phone)
    {
        readerMapper.deleteReader(phone);
    }
    //时间转为字符串
    public String TimeToString(Calendar calendar){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = calendar.getTime();
        String dateStringParse = sdf.format(date);
        return dateStringParse;
    }

    public void updatefine(String phone){
        readerMapper.updatefine(phone);
    }
}

